package com.lw.helloWordDemo;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;

import java.io.PrintWriter;

public class VelocityDemo {
	
	public void initVecocity() {
		 VelocityEngine ve=new VelocityEngine();
		 //设置模板加载路径，这里设置的是class下  
		 ve.setProperty(Velocity.RESOURCE_LOADER, "class");
		 ve.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
	        try {   
	            //进行初始化操作   
	            ve.init();
				/**
				 * .vm模板文件尽量放在resources中，更好读取，放到java下还得改pom.xml
				 */
				//加载模板，设定模板编码
	            Template t=ve.getTemplate("/templates/initVecocity.vm","gbk");
	            //设置初始化数据   
	            VelocityContext context = new VelocityContext();
	            //设置输出   
	            PrintWriter writer = new PrintWriter("D:\\helloword.html");
	            //将环境数据转化输出   
	            t.merge(context, writer);   
	            writer.close();   
	        } catch (Exception e) {   
	            e.printStackTrace();   
	        }  
	}
}